﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace WebSys.UI.SysManage
{
    public class LoggerList : Page
    {
        private MPRepeater LoggerMPRepeater, LoggerInfoMPRepeater;
        private Button viewbtn;
        private MPCalendar loggerdate;
        private DropDownList loggertype;

        protected override void OnLoad(EventArgs e)
        {
            InitControl();
            base.OnLoad(e);
            if (!IsPostBack)
            {
                string date = DateTime.Now.ToShortDateString().Replace("/", "-");
                Initcy(loggertype.SelectedValue, date);
                loggerdate.Text = date;
            }
        }

        private void InitControl()
        {
            LoggerMPRepeater = (MPRepeater) FindControl("LoggerMPRepeater");
            LoggerInfoMPRepeater = (MPRepeater) FindControl("LoggerInfoMPRepeater");
            viewbtn = (Button) FindControl("viewbtn");
            loggerdate = (MPCalendar) FindControl("loggerdate");
            loggertype = (DropDownList) FindControl("loggertype");
            viewbtn.Click += viewbtn_Click;
        }

        private void viewbtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(loggerdate.Text))
            {
                Initcy(loggertype.SelectedValue, loggerdate.Text);
            }
        }

        private void Initcy(string loggertype, string datetime)
        {
            string filepath = Server.MapPath("~/Log/" + datetime + ".xml");
            if (loggertype == "信息日志")
            {
                filepath = Server.MapPath("~/Log/" + datetime + "_info.xml");
                LoggerInfoMPRepeater.Visible = true;
                LoggerMPRepeater.Visible = false;
                if (!File.Exists(filepath))
                {
                    LoggerInfoMPRepeater.DataSource = null;
                    LoggerInfoMPRepeater.DataBind();
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(filepath);
                    DataView dr = ds.Tables["InfoTime"].DefaultView;
                    dr.Sort = "value desc";
                    LoggerInfoMPRepeater.DataSource = dr;
                    LoggerInfoMPRepeater.DataBind();
                }
            }
            else if (loggertype =="SQL操作日志")
            {
                filepath = Server.MapPath("~/Log/" + datetime + "_sql.xml");
                LoggerInfoMPRepeater.Visible = true;
                LoggerMPRepeater.Visible = false;
                if (!File.Exists(filepath))
                {
                    LoggerInfoMPRepeater.DataSource = null;
                    LoggerInfoMPRepeater.DataBind();
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(filepath);
                    DataView dr = ds.Tables["InfoTime"].DefaultView;
                    dr.Sort = "value desc";
                    LoggerInfoMPRepeater.DataSource = dr;
                    LoggerInfoMPRepeater.DataBind();
                }
            }
           else
            {
                filepath = Server.MapPath("~/Log/" + datetime + ".xml");
                LoggerInfoMPRepeater.Visible = false;
                LoggerMPRepeater.Visible = true;
                if (!File.Exists(filepath))
                {
                    LoggerMPRepeater.DataSource = null;
                    LoggerMPRepeater.DataBind();
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(filepath);
                    DataView dr = ds.Tables["ErrorTime"].DefaultView;
                    dr.Sort = "value desc";
                    LoggerMPRepeater.DataSource = dr;
                    LoggerMPRepeater.DataBind();
                }
            }
        }
    }
}